home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / fwrite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.3 KB  |  68 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stddef.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <limits.h>
  8. #include <assert.h>
  9. #include <string.h>
  10. #include <memory.h>
  11.  
  12. size_t  fwrite(data, size, count, fp)
  13. _CONST register void *data;
  14. size_t size;
  15. size_t count;
  16. register FILE *fp;
  17. {
  18.     register unsigned long n, m;
  19.     register long l = 0;
  20.     long space;
  21.     unsigned int f = fp->_flag;
  22.     
  23.     if(f & _IORW)
  24.     {
  25.     fp->_flag |= _IOWRT;
  26.     f = (fp->_flag &= ~(_IOREAD | _IOEOF));
  27.     }
  28.     
  29.     if(!(f & _IOWRT)            /* not opened for write? */
  30.        || (f & (_IOERR | _IOEOF)))        /* error/eof conditions? */
  31.     return(0);
  32.     
  33.     assert ((data != NULL));
  34.     assert ((size != 0));
  35.     n =  (unsigned long)count * (unsigned long)size;
  36.     assert ( n <= LONG_MAX);  /* otherwise impl will not work */
  37.     
  38.     space = fp->_bsiz - fp->_cnt;
  39.     while(n > 0)
  40.     {
  41.     m = (n > space)? space: n;
  42.     bcopy(data, fp->_ptr, m);
  43.     fp->_ptr += m;
  44.     fp->_cnt += m;
  45.     space -= m;
  46.     if(space == 0)
  47.     {
  48.         if(fflush(fp))
  49.         return 0;
  50.         space = fp->_bsiz;
  51.     }
  52.     l += m;
  53.     data += m;
  54.     n -= m;
  55.     if(n < space)
  56.         continue;
  57.     if((m = lwrite(fp->_file, data, (long)n)) != (long)n)
  58.     {
  59.         fp->_flag |= _IOERR;
  60.         return 0;
  61.     }
  62.     l += m;
  63.     break;
  64.     }
  65.     
  66.     return((l > 0) ? (size_t)((unsigned long)l / (unsigned long)size) : 0);
  67. }
  68.